home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 592b.lha / TermII / Fran軋is / Exemples XCMD / panel.c next >
C/C++ Source or Header  |  1991-12-22  |  9KB  |  398 lines

  1. #include <exec/types.h>
  2. #include <exec/libraries.h>
  3. #include <exec/ports.h>
  4. #include <intuition/intuition.h>
  5. #include <libraries/gadtools.h>
  6. #include <clib/exec_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/gadtools_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/alib_protos.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include "XCMD.h"
  15. #include "XCMDTools.h"
  16.  
  17. #define forever for(;;)
  18.  
  19. /*
  20. *     Les principales variables :
  21. */
  22. struct GadgetList *glist         = NULL;
  23. struct Window     *window        = NULL;
  24. struct Screen     *screen_lock   = NULL;
  25. void              *vi            = NULL;
  26. struct MsgPort    *port          = NULL;
  27. BPTR              dir_lock;
  28. struct Library    *GadToolsBase  = NULL;
  29. struct TextAttr   Topaz80        = { "topaz.font",8,0,0 };
  30. char              ligne[256];
  31. struct Panel
  32.   {
  33.   BOOL pa_Set;
  34.   char pa_Texte[10];
  35.   char pa_Commande[256];
  36.   };
  37. struct Panel      Panel[25];
  38. int               PanelIX = 0;
  39.  
  40.  
  41. /*
  42. *
  43. *       prototypes des fonctions utilisées
  44. *
  45. */
  46.  
  47. void Load(void);
  48. void InterpreterLigne(void);
  49. BOOL InitPanel(void);
  50.  
  51.  
  52. /*
  53. *       void InterpreterLigne(void)
  54. *
  55. *       Interprète la variable _ligne_ et associe le texte
  56. *       et la commande à un gadget du panel.
  57. *
  58. */
  59. void InterpreterLigne()
  60.   {
  61.   int i = 0;
  62.   while((ligne[i] == ' ') && (ligne[i]))
  63.     i++;
  64.   if(ligne[i])
  65.     {
  66.     int j = 0;
  67.     while((ligne[i] != ';') && (ligne[i]))
  68.       {
  69.       if(j<9)
  70.         Panel[PanelIX].pa_Texte[j] = ligne[i];
  71.       else if(j==9)
  72.         Panel[PanelIX].pa_Texte[j] = '\0';
  73.       i++;
  74.       j++;
  75.       }
  76.     if((ligne[i] == ';') && (j<9))
  77.       Panel[PanelIX].pa_Texte[j] = '\0';
  78.     if(ligne[i])
  79.       {
  80.       i++;
  81.       while((ligne[i] == ' ') && (ligne[i]))
  82.         i++;
  83.       if(ligne[i])
  84.         {
  85.         int k = 0;
  86.         while(ligne[i])
  87.           {
  88.           Panel[PanelIX].pa_Commande[k] = ligne[i];
  89.           i++; k++;
  90.           }
  91.         Panel[PanelIX].pa_Commande[k] = ligne[i];
  92.         Panel[PanelIX].pa_Set = TRUE;
  93.         PanelIX++;
  94.         }
  95.       }
  96.     }
  97.   }
  98.  
  99.  
  100.  
  101.  
  102. /*
  103. *
  104. *       void Load(void)
  105. *
  106. *       Charge le fichier "panel.config" pour définir les
  107. *       commandes associés au gadget. Chaque ligne est lue
  108. *       dans la variable _ligne_ qui est ensuite interprétée
  109. *
  110. */
  111. void Load()
  112.   {
  113.   FILE *fp;
  114.   int i,c;
  115.   for(i = 0; i<25; i++)
  116.     Panel[i].pa_Set = FALSE;
  117.   fp = fopen("panel.config","r");
  118.   if(fp)
  119.     {
  120.     forever         /* lecture jusqu'à EOF ou erreur */
  121.       {
  122.       i = 0;
  123.       forever
  124.         {
  125.         c = fgetc(fp);
  126.         if((c == EOF) || (c == '\n') || (i == 255))
  127.           {
  128.           ligne[i] = '\0';
  129.           break;
  130.           }
  131.         else
  132.           {
  133.           ligne[i] = c;
  134.           i++;
  135.           }
  136.         }
  137.       if(strlen(ligne) && (ligne[0] != '#'))
  138.         InterpreterLigne();
  139.       if ((c == EOF) || (PanelIX == 25)) break;
  140.       }
  141.     fclose(fp);
  142.     }
  143.   }
  144.  
  145.  
  146.  
  147. /*
  148. *       InitPanel()
  149. *
  150. *       Cette routine initialise le Panel, en lisant
  151. *       un fichier de configuration "panel.config".
  152. *       Une fenêtre est ouverte avec tous les gadgets
  153. *       nécessaires. Retourne TRUE si tout est OK,
  154. *       FALSE sinon.
  155. *
  156. */
  157. BOOL InitPanel()
  158.   {
  159.   vi = GetVisualInfo(screen_lock, TAG_DONE);
  160.   if(!vi)
  161.     {
  162.     puts("Impossible d'avoir les visual infos");
  163.     return(FALSE);
  164.     }
  165.   window = OpenWindowTags(NULL,
  166.     WA_DragBar, TRUE,
  167.     WA_DepthGadget, TRUE,
  168.     WA_PubScreen, screen_lock,
  169.     WA_Width, 417,
  170.     WA_Height, 90,
  171.     WA_CloseGadget, TRUE,
  172.     WA_IDCMP, CLOSEWINDOW|REFRESHWINDOW|BUTTONIDCMP,
  173.     WA_Title, "Command panel",
  174.     WA_Activate, TRUE,
  175.     TAG_DONE);
  176.   if(!window)
  177.     {
  178.     puts("Impossible d'ouvrir la fenetre");
  179.     return(FALSE);
  180.     }
  181.  
  182.   struct Gadget *gad = CreateContext(&glist);
  183.   struct NewGadget ng;
  184.  
  185.   /*
  186.   *   On crée la liste pour les touches de fonction
  187.   */
  188.  
  189.   ng.ng_TextAttr = &Topaz80;
  190.   ng.ng_VisualInfo = vi;
  191.   ng.ng_LeftEdge = 5;
  192.   ng.ng_TopEdge = 12;
  193.   ng.ng_Width = 80;
  194.   ng.ng_Height = 14;
  195.   ng.ng_Flags = 0;
  196.   ng.ng_GadgetText = NULL;
  197.  
  198.   for(int j=0; j<5; j++)
  199.     {
  200.     for(int i = 0; i<5; i++)
  201.       {
  202.       int k = j*5 + i;
  203.       ng.ng_GadgetID = k;
  204.       if(Panel[k].pa_Set)
  205.         ng.ng_GadgetText = Panel[k].pa_Texte;
  206.       else
  207.         ng.ng_GadgetText = NULL;
  208.       gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  209.       ng.ng_LeftEdge += 82;
  210.       }
  211.     ng.ng_TopEdge += 15;
  212.     ng.ng_LeftEdge = 5;
  213.     }
  214.  
  215.   if(!gad)
  216.     {
  217.     puts("Impossible de créer les gadgets");
  218.     return(FALSE);
  219.     }
  220.  
  221.   AddGList(window,glist,-1,-1,NULL);
  222.   RefreshGList(glist,window,NULL,-1);
  223.   GT_RefreshWindow(window,NULL);
  224.   return(TRUE);
  225.   }
  226.  
  227.  
  228.  
  229.  
  230. /*
  231. *
  232. *           main()
  233. *
  234. *
  235. */
  236.  
  237. int main()
  238.  
  239.   {
  240.   /*
  241.   *     On vérifie d'abord que nous sommes unique !
  242.   */
  243.   if(FindPort("PANEL_PORT"))
  244.     {
  245.     puts("Mais j'existe déjà !!!");
  246.     exit(10);
  247.     }
  248.   /*
  249.   *     On ouvre la gadget toolkit
  250.   */
  251.   GadToolsBase = OpenLibrary("gadtools.library",36);
  252.   if(!GadToolsBase)
  253.     {
  254.     puts("Impossible d'ouvrir la gadget toolkit");
  255.     exit(10);
  256.     }
  257.   /*
  258.   *     Création du port de Panel
  259.   */
  260.   port = CreatePort("PANEL_PORT",0);
  261.   if(!port)
  262.     {
  263.     puts("Impossible d'ouvrir PANEL_PORT");
  264.     CloseLibrary(GadToolsBase);
  265.     exit(10);
  266.     }
  267.   /*
  268.   *     Maintenant on attend une commande
  269.   *     START en provenance de l'utilisateur
  270.   *     de Term II
  271.   */
  272.   forever
  273.     {
  274.     WaitPort(port);
  275.     struct XCMD *xcmd = GetMsg(port);
  276.     if(xcmd && (xcmd->xcmd_TermCmd == XCMD_START))
  277.       {
  278.       screen_lock = xcmd->xcmd_TermScreen;
  279.       dir_lock    = xcmd->xcmd_TermDir;
  280.       ReplyMsg(xcmd);
  281.       break;
  282.       }
  283.     else
  284.       ReplyMsg(xcmd);
  285.     }
  286.   /*
  287.   *     On se place dans le même répertoire
  288.   *     que Term II pour lire le fichier de
  289.   *     configuration. Mais on sauve le
  290.   *     répertoire courant dans old_lock
  291.   */
  292.   BPTR old_lock = CurrentDir(dir_lock);
  293.   /*
  294.   *     Et on lit le fichier de configuration
  295.   */
  296.   Load();
  297.   /*
  298.   *     Et on affiche le panel
  299.   */
  300.   struct XCMD *xcmd = CreateXCMD(port);
  301.   if(InitPanel() && xcmd)
  302.     {
  303.     BOOL pas_fini = TRUE;
  304.     struct IntuiMessage *msg;
  305.     ULONG IntuiSignal = 1 << window->UserPort->mp_SigBit;
  306.     ULONG XCMDSignal  = 1 << port->mp_SigBit;
  307.     while(pas_fini)
  308.       {
  309.       ULONG signaux = Wait(IntuiSignal|XCMDSignal);
  310.       if(signaux & IntuiSignal)
  311.         {
  312.         /*
  313.         *       Nous avons reçu un signal depuis Intuition
  314.         */
  315.         while(msg = GT_GetIMsg(window->UserPort))
  316.           {
  317.           ULONG class = msg->Class;
  318.           SHORT code = msg->Code;
  319.           struct Gadget *gadget = msg->IAddress;
  320.           GT_ReplyIMsg(msg);
  321.           switch(class)
  322.             {
  323.             case CLOSEWINDOW:
  324.               pas_fini = FALSE;
  325.               break;
  326.             case REFRESHWINDOW:
  327.               GT_BeginRefresh(window);
  328.               GT_EndRefresh(window,TRUE);
  329.               break;
  330.             case GADGETUP:
  331.               /*
  332.               *     Un gadget a été sélectionné :
  333.               *     on envoie la commande associée
  334.               */
  335.               xcmd->xcmd_Command = Panel[gadget->GadgetID].pa_Commande;
  336.               if(SendXCMD(xcmd))
  337.                 {
  338.                 /*
  339.                 *     Il _faut_ attendre la réponse
  340.                 *     de Term II
  341.                 */
  342.                 WaitPort(port);
  343.                 GetMsg(port);
  344.                 }
  345.               /*
  346.               *     Voilà, c'est fini...
  347.               */
  348.               break;
  349.             }
  350.           }
  351.         }
  352.       if(signaux & XCMDSignal)
  353.         {
  354.         /*
  355.         *       Nous avons reçu une message depuis
  356.         *       Term II. Il ne peut s'agir que d'une
  357.         *       commande QUIT, ou d'un START envoyer
  358.         *       par erreur à l'utilisateur.
  359.         */
  360.         struct XCMD *x = GetMsg(port);
  361.         if(x->xcmd_TermCmd == XCMD_STOP)
  362.           pas_fini = FALSE;
  363.         else
  364.           {
  365.           /*
  366.           *     C'est un STOP envoyé par erreur.
  367.           *     On enlève les lock, et on retourne
  368.           *     le message à Term II
  369.           */
  370.           UnLock(x->xcmd_TermDir);
  371.           UnlockPubScreen(NULL,x->xcmd_TermScreen);
  372.           }
  373.         ReplyMsg(x);
  374.         }
  375.       }
  376.     }
  377.   if(xcmd)
  378.     DeleteXCMD(xcmd);
  379.   if(port)
  380.     DeletePort(port);
  381.   if(window)
  382.     {
  383.     struct IntuiMessage *msg;
  384.     while(msg = GT_GetIMsg(window->UserPort)) GT_ReplyIMsg(msg);
  385.     CloseWindow(window);
  386.     };
  387.   if(vi)
  388.     FreeVisualInfo(vi);
  389.   if(GadToolsBase)
  390.     {
  391.     FreeGadgets(glist);
  392.     UnlockPubScreen(NULL,screen_lock);
  393.     CloseLibrary(GadToolsBase);
  394.     }
  395.   CurrentDir(old_lock);
  396.   UnLock(dir_lock);
  397.   }
  398.